package main

import (
	"fmt"
)

func main() {
	fmt.Println(mySqrt1(16))
}

// mySqrt1 二分查找方法计算
// 5 :0到5
// 0  5 -> 5/2 *5/2 = 4
func mySqrt1(x int) int {
	if x == 1 {
		return 1
	}

	if x == 0 {
		return 0
	}

	var p1, p2 int
	ans := -1
	p1, p2 = 0, x

	for p2-p1 > 0 {
		mid := (p1 + p2) / 2
		if mid*mid > x {
			p2 = mid
		} else {
			ans = int(mid)
			p1 = mid + 1
		}
	}

	return ans
}
